package com.example.contentproviderdemo;


import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class MyProvider extends ContentProvider{
	
	MySQLiteOpenHelper mSQLiteOpenHelper;
	
	int count =1;
	
	

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = mSQLiteOpenHelper.getWritableDatabase();
		int id = db.delete(Persons.TABLE_NAME, selection, selectionArgs);
		return id;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = mSQLiteOpenHelper.getWritableDatabase();
		long id = db.insert(Persons.TABLE_NAME, null, values);
		return ContentUris.withAppendedId(uri, id);
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db=null;
		if (count==1){
			db = mSQLiteOpenHelper.getWritableDatabase();
			mSQLiteOpenHelper.insert();
			count++;
		} else {
			db =  mSQLiteOpenHelper.getReadableDatabase();
			Cursor cursor = db.query(Persons.TABLE_NAME, projection,  
                    selection, selectionArgs, null, null, sortOrder);  
            return cursor;  
		}
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mSQLiteOpenHelper.getWritableDatabase();  
        int id = db  
                .update(Persons.TABLE_NAME, values, selection, selectionArgs);  
        return id;
	}

}
